﻿using Microsoft.AspNetCore.Http;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Caching.Distributed;
using Microsoft.Extensions.Logging;

namespace Devonline.AspNetCore;

/// <summary>
/// Provides services for managing and handling operations related to <see cref="Personal"/> entities,  including
/// support for attachments and integration with distributed caching, HTTP context, and file services.
/// </summary>
/// <remarks>This service extends <see cref="DataWithAttachmentService{TDbContext, TEntity}"/> to provide
/// additional  functionality specific to <see cref="Personal"/> entities. It integrates with various services such as 
/// logging, distributed caching, HTTP context access, and file management to support advanced operations.</remarks>
/// <typeparam name="TDbContext">The type of the database context used by the service. Must derive from <see cref="DbContext"/>.</typeparam>
/// <param name="logger"></param>
/// <param name="context"></param>
/// <param name="cache"></param>
/// <param name="httpContextAccessor"></param>
/// <param name="fileService"></param>
/// <param name="httpSetting"></param>
public class PersonalService<TDbContext>(
    ILogger<PersonalService<TDbContext>> logger,
    TDbContext context,
    IDistributedCache cache,
    IHttpContextAccessor httpContextAccessor,
    IFileService fileService,
    HttpSetting httpSetting) :
    DataWithAttachmentService<TDbContext, Personal>(logger, context, cache, httpContextAccessor, fileService, httpSetting),
    IDataWithAttachmentService<TDbContext, Personal>
    where TDbContext : DbContext
{
    /// <summary>
    /// 从上下文关联用户获取个人信息
    /// </summary>
    /// <returns></returns>
    /// <exception cref="BadHttpRequestException"></exception>
    public virtual async Task<Personal> GetAsync() => await FirstOrDefaultAsync(x => x.State == DataState.Available && x.Id == UserId) ?? throw new BadHttpRequestException("未能查询到关联用户个人信息");
}